clear all 
capture log close

global data "/Users/ceweber/Dropbox/JPAM_TIV_code/original_data/"
global dta "/Users/ceweber/Dropbox/JPAM_TIV_code/dta_files/"
global dataclean "/Users/ceweber/Dropbox/JPAM_TIV_code/dta_output/"
global output "/Users/ceweber/Dropbox/JPAM_TIV_code/output/"

set more off
cd "${dataclean}"

//////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////
//////////////////STAGE FOUR////////////////////////////////
//////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////
use rounding_results_weekly_expand_kendall2
sum group
local locationmax = r(max)
//loop through each location
set more off 
 forvalues l =1(1)`locationmax'{

 use inv_formerging_`l'.dta, clear

 
 if _N==1{

		gen final_mult_new=final_mult
		save multiplier_details_`l'.dta, replace
	}
  
 if _N==1 continue
 egen p10 = pctile(max_round), p(10)


 gen clean = 1 
 replace clean = 0 if max_round<p10

 
 
 //create a use_dominant flag here based on unclean data -- if we drop more than half of the data in any given week from this cleaning, then this is a week in which just defaults to whatever is dominant IF its in a non-dominant region: 
 gen use_dominant=0
 gen weekly = wofd(date+1)
 egen mean_clean=mean(clean), by(week)
 replace use_dominant=1  if mean_clean<.5

 
	//work with only the first ob for the inventorylot - multiplier pair (this keeps the first ob after transition as well)
		sort inventoryid date
		replace final_mult=round(final_mult,.001)
		egen inv_multiplier_group = group(inventoryid final_mult)
		sort inventoryid inv_multiplier_group date
		bysort inventoryid inv_multiplier_group: gen first = 1 if _n==1
		sort inventoryid inv_multiplier_group date
		bysort inventoryid inv_multiplier_group: gen last = 1 if _n==_N
		
	
		//i'm now constructing a daily tracker of multiplier. stata gets tricky with all these inventoryids hanging around, so im making a daily key and then merging everything back in 
preserve
			
			keep if first==1|last==1
			drop if final_mult==.
			keep if clean==1
	
					
		//MAKE SOME STATS ABOUT HOW OFTEN ALL THE MULTIPLIERS SHOW UP BY WEEK 
		
			tab final_mult, gen(mult)
			egen group_finalmult = group(final_mult)
			sum group_finalmult 
			local max = r(max)
			local finalmult_max = r(max)
			
			//label these counts with the appropriate multiplier number
			format final_mult %09.2f
			forvalues j = 1(1)`finalmult_max'{
			gen mult_num`j' = final_mult if group==`j'
			sum mult_num`j'
			local mean = r(mean)
			local name = round(r(mean), .001)
			gen tmp=round(r(mean),.001)
			drop mult_num`j' 
			gen mult_num`j'=round(tmp,.001)
			drop tmp
			}
			di `finalmult_max'
			 
	
		
		foreach x in first last{
			//sum of multiplier occurence by day 
			forvalues j = 1(1)`finalmult_max'{
			egen sum_mult`x'`j'  = total(mult`j') if `x'==1, by(weekly)
			}	
			
			//make a percent, not an absolute count 
			gen total_sum`x'_week = 0
			forvalues j = 1(1)`finalmult_max'{
			qui replace total_sum`x'_week = total_sum`x'_week + sum_mult`x'`j'
			}
		
			forvalues j = 1(1)`finalmult_max'{
			gen percent`x'_`j' = sum_mult`x'`j'/total_sum`x'_week
			}
			
			//label these counts with the appropriate multiplier number
			format final_mult %09.2f
			forvalues j = 1(1)`finalmult_max'{
			sum mult_num`j'
			local mean = r(mean)
			local name = round(r(mean), .001)
			label var percent`x'_`j' "mult = `name'"
			}
	
			//figure out the max percent by day 
			gen max_percent`x' = 0 if `x'==1
			forvalues j = 1(1)`finalmult_max'{
			replace max_percent`x' = percent`x'_`j' if percent`x'_`j'>max_percent`x'&percent`x'_`j'!=.&`x'==1
			}
		
			//figure out second best percent 
			gen second_max_percent`x' = 0 if `x'==1
			forvalues j = 1(1)`finalmult_max'{
			replace second_max_percent`x' = percent`x'_`j' if percent`x'_`j'>second_max_percent`x'&percent`x'_`j'!=.&percent`x'_`j'!=max_percent`x'&`x'==1
			}
			}

		//collapse to one ob per week 
		collapse (mean) max_percent* second_max_percent* mult_num* percent*_* location use_dominant, by(weekly) fast
	 
	  
		forvalues j = 1(1)`finalmult_max'{
		replace mult_num`j'=round(mult_num`j',.001)
		}
		
			
	/*  count 
 tsset weekly
 tsfill 
 count
replace use_dominant=1 if use_dominant==.*/
 
		 
			//we lost the label in the collapse, going to gen it again 
			forvalues j = 1(1)`finalmult_max'{
				qui sum mult_num`j'
				local name = round(r(mean), .001)
				label var percentfirst_`j' "mult = `name'"
				label var percentlast_`j' "mult = `name'"
				}	
				
				
		//NOW PICK THE SET OF MULTIPLIERS	
		//pick dominant if max_percent is at least 20% better than second_max_percent (based on first multiplier inventory lot date)
		gen dominant = . 
		forvalues j = 1(1)`finalmult_max'{
		replace mult_num`j'=round(mult_num`j',.001)
		replace dominant = mult_num`j' if percentfirst_`j'==max_percentfirst&(max_percentfirst - second_max_percentfirst)>.2
		}
		
		//if there is a short spell of non-dominance (1 week), surrounded by two longer spells of dominance, fill it in (also kicks out one week periods of dominance -- only should do that after firm has been open for four weeks because could be short spells at the beginning that we want to keep)
	if _N>1{
		sort weekly
		gen run = _n
		tsset run
		tsspell dominant
		bysort _spell: gen length = _N
		sort weekly
		replace dominant = . if length==1 //&_n>4
		//replace dominant=. if length==1&_n<=4&(length[_n+1]<=2|length[_n-1]<=2)
		carryforward dominant if length==1&length[_n-1]>=2&length[_n+1]>=2&dominant[_n+1]==dominant[_n-1], replace 
		drop run length _end _seq _spell
		summ dominant if location==.
		}
		
		//create a flag so we know when a dominant multiplier has been assigned.  
		gen dom_flag = 0 
		replace dom_flag = 1 if dominant!=.
		label var dom_flag "dominant multiplier assigned"
		tab dom_flag //check to make sure always one or zero, in the middle means something weird happened with assigning 
		
		//next we define transitions between dominant periods so that we don't consider these as candidate ambiguous periods.  
		gsort -weekly
		gen transition_period = 0 
		gen transition_period2 = 0
		gen transition_mult6 = 0 
		gen transition_mult7 = 0 
			
		gsort -weekly
		replace transition_period=1 if (dominant[_n]!=dominant[_n-1])&dom_flag[_n-1]==1&dom_flag[_n]==1&weekly[_n]!=.&weekly[_n-1]!=.
		replace transition_mult6=dominant[_n-1] if (dominant[_n]!=dominant[_n-1])&dom_flag[_n-1]==1&dom_flag[_n]==1&weekly[_n]!=.&weekly[_n-1]!=.

		sort weekly
		replace transition_period=1 if (dominant[_n]!=dominant[_n-1])&dom_flag[_n-1]==1&dom_flag[_n]==1&weekly[_n]!=.&weekly[_n-1]!=.
		replace transition_mult7=dominant[_n-1] if (dominant[_n]!=dominant[_n-1])&dom_flag[_n-1]==1&dom_flag[_n]==1&weekly[_n]!=.&weekly[_n-1]!=.
	 
		//lead in period (based on first multiplier inventory lot date)
		forvalues j = 1(1)1{
		gsort -weekly
		replace transition_mult6 = transition_mult6[_n-1] if transition_mult6[_n-1]==mult_num`j'&percentfirst_`j'>=.1 &mult_num`j'!=dominant&transition_mult6[_n-1]!=.&weekly[_n]!=.&weekly[_n-1]!=.  //&(mult_num`j'==dominant[_n-1])
		replace transition_mult7 = transition_mult7[_n-1] if transition_mult7[_n-1]==mult_num`j'&percentfirst_`j'>=.1 &mult_num`j'!=dominant&transition_mult7[_n-1]!=.&weekly[_n]!=.&weekly[_n-1]!=.   //&(mult_num`j'==dominant[_n-1])
		}
		 
		
		//lead out period (based on last multiplier inventory lot date)
		sort weekly
		forvalues j = 1(1)`finalmult_max'{
		replace transition_mult6 = transition_mult6[_n-1] if transition_mult6[_n-1]==mult_num`j'&percentlast_`j'>=.1 &mult_num`j'!=dominant[_n]&transition_mult6[_n-1]!=.&weekly[_n]!=.&weekly[_n-1]!=.  //&(mult_num`j'==dominant[_n-1])
		replace transition_mult7 = transition_mult7[_n-1] if transition_mult7[_n-1]==mult_num`j'&percentlast_`j'>=.1 &mult_num`j'!=dominant[_n]&transition_mult7[_n-1]!=.&weekly[_n]!=.&weekly[_n-1]!=.  //&(mult_num`j'==dominant[_n-1])
		
		}
 
		replace transition_period = 1 if (transition_mult6!=0&transition_mult6<.)|(transition_mult7!=0&transition_mult7<.)
	
		
		//identify periods of non-dominance. we will do something with these in a minute.
		gen ambiguous_period = 0 
		replace ambiguous_period = 1 if dom_flag==0&transition_period==0
		
		
		//deal with ambiguous bordering transition periods from dominant regions -- these need to be coded as a different transition period so we can transition from the ambiguous multipliers to the dominant one
		gen transition_period_ambiguous=0
		sort weekly
		replace transition_period_ambiguous = 1 if ambiguous_period[_n-1]==1&transition_period== 1
		gsort -weekly
		replace transition_period_ambiguous = 1 if ambiguous_period[_n-1]==1&transition_period== 1
		replace transition_period = 0 if transition_period_ambiguous ==1
		
		//and for now i am going to put these "transition periods" back into the ambiguous period and then we will sort out the transition a bit later:
		replace ambiguous_period=1 if transition_period_ambiguous==1
		
		//we may want to expand out the transition periods.  if we do, this code does that-- i think i want to modify this eventually so that this code takes into account percentages when expanding maybe: 
				gsort -weekly
			gen tmp=transition_mult6[_n-1] if transition_period[_n-1]==1 &weekly[_n]!=.
			replace  tmp=transition_mult6[_n-2] if transition_period[_n-2]==1&transition_period[_n-1]==0 &weekly[_n]!=.
			replace transition_mult6=tmp if tmp!=.&tmp!=0
			
			gsort weekly
			gen tmp2=transition_mult7[_n-1] if transition_period[_n-1]==1 &weekly[_n]!=.
			replace  tmp2=transition_mult7[_n-2] if transition_period[_n-2]==1&transition_period[_n-1]==0 &weekly[_n]!=.
			replace transition_mult7=tmp2 if tmp2!=.&tmp2!=0
		
			drop tmp tmp2
			replace transition_period = 1 if transition_mult6!=0|transition_mult7!=0
			replace transition_period = 0 if ambiguous_period ==1
				drop transition_period2
			replace dom_flag=0 if transition_period==1
		
		//and absorb the neighboring transition region into ambiguous region too. also if it ends on a transition, switch this to an ambiguous period:
		sort weekly
		replace ambiguous_period=1 if transition_period==1&weekly[_n-1]==.
		replace ambiguous_period=1 if transition_period==1&ambiguous_period[_n-1]==1
		
		gsort -weekly
		replace ambiguous_period=1 if transition_period==1&weekly[_n-1]==.
		replace ambiguous_period=1 if transition_period==1&ambiguous_period[_n-1]==1
		
		replace transition_period = 0 if transition_period_ambiguous ==1
		replace ambiguous_period=1  if transition_period_ambiguous ==1
		replace transition_period=0 if ambiguous_period==1
		
		
		//now we get rid of short dominant regions surrounded by ambiguous regions:
			if _N>1{
			sort weekly 
		gen run = _n 
		tsset run 
		tsspell dom_flag
		bysort _spell: gen length = _N
		gen ambiguous_before = ambiguous_period[_n-1] if _seq==1&dom_flag==1
		gen ambiguous_after = ambiguous_period[_n+1] if _end==1&dom_flag==1
		egen ambb4tmp = mean(ambiguous_before), by(_spell)
		egen ambbaftertmp =mean(ambiguous_after), by(_spell)
		replace ambiguous_before = round(ambb4tmp,.001)
		replace ambiguous_after = round(ambbaftertmp,.001)
		drop ambb4tmp ambbaftertmp
		
	
		
		replace dominant = . if length<=4&dom_flag==1&ambiguous_before==1&ambiguous_after==1
		replace ambiguous_period=1 if length<=4&dom_flag==1&ambiguous_before==1&ambiguous_after==1
		replace dom_flag =0 if length<=4&dom_flag==1&ambiguous_before==1&ambiguous_after==1
		
		//and also short dominant followed by long transition period:
 
		
		drop run length _end _seq _spell ambiguous_before ambiguous_after
		}
		
		//now we have dominant multipliers defined, transition periods where we can choose between dominant before and dominant after.  and ambiguous periods which are still unknown	
	
		//ok now we need to think about multiplier in ambiguous region and narrow down to the appropriate multiplier set:
		
		//first return to use_dominant var from above and replace use_dominant=0 if we were able to identify a dominant or transition region from this week:
			if _N>1{
		replace use_dominant=0 if dom_flag==1|transition_period==1
		sort weekly 
		gen run = _n 
		tsset run 
		tsspell ambiguous_period
		summ _spell
		
		replace _spell=. if ambiguous_period==0
		gen spells=.
		replace spells=1 if _spell>=1&_spell<.
		summ spells
		replace spells=r(mean)
		gen dominant2=.
		if spells==1{
			egen spellgroup=group(_spell) 
			sum spellgroup 
			local spellmax=r(max)
			forvalues i=1(1)`spellmax'{
			//based on non-roundedness of the spell (75% or more were defined as weeks in which the dominant multiplier should be used) define spells that should default to dominant multipliers from either side:
			summ use_dominant if spellgroup==`i'
			replace use_dominant=1 if spellgroup==`i'&r(mean)>=.75
			replace use_dominant=0 if spellgroup==`i'&r(mean)<.75
			//next define another set of cases where multiplier is unknown (we will define as max percentage from any mult as less than 33%) and dominant multiplier from either side should be used:
			summ max_percentfirst if spellgroup==`i'
			replace use_dominant=1 if spellgroup==`i'&r(mean)<=.33
			}
			//also unknown if in this ambiguous period more than two diff multipliers are dominant for any given week:
			gen tmpdominant=.
			gen tmpdominantpercent=.
			forvalues i=1(1)`spellmax'{
			forvalues j = 1(1)`finalmult_max'{
			replace tmpdominant= mult_num`j' if percentfirst_`j'==max_percentfirst&spellgroup==`i'&transition_mult6!=mult_num`j'&transition_mult7!=mult_num`j'
			replace tmpdominantpercent= percentfirst_`j' if percentfirst_`j'==max_percentfirst&spellgroup==`i'&transition_mult6!=mult_num`j'&transition_mult7!=mult_num`j'
			}
			}
			egen tag=tag(tmpdominant spellgroup)
			replace tag=. if tmpdominant==.|transition_mult6
			egen tagtot=total(tag) ,by(spellgroup)
			
			replace use_dominant=1 if spellgroup<.&tagtot>2&tagtot<.
			
			//now if 2 multipliers or less define these two multipliers.  put the highest percent var on average in the dominant var slot and the second highest in the second highest slot (we may later kick out second highest if it doesn't actually garner a larger enough fraction of transactions?)
			gen dom_1=.
			gen dom_2=. 
			gen percentdom_1=.
			gen percentdom_2=.
			
	
			forvalues i=1(1)`spellmax'{
			capture noisily tab tmpdominant if use_dominant==0&ambiguous_period==1&spellgroup==`i', gen(tmp_)
			capture noisily replace dom_1=tmp_1 if use_dominant==0&ambiguous_period==1&spellgroup==`i'
			capture noisily replace dom_2=tmp_2 if use_dominant==0&ambiguous_period==1&spellgroup==`i'
			
			forvalues j=1(1)2{
			*summ dom_`j' if spellgroup==`i'
			*replace dom_`j'=round(r(mean), .001) if spellgroup==`i'
			summ tmpdominantpercent if dom_`j'==1&spellgroup==`i'
			replace percentdom_`j'=round(r(mean), .001) if spellgroup==`i'&use_dominant==0&ambiguous_period==1
			summ tmpdominant if dom_`j'==1&spellgroup==`i'
			replace dom_`j'=round(r(mean),.001) if spellgroup==`i'&use_dominant==0&ambiguous_period==1
			capture noisily drop tmp_*
			}
			}
			
			
			replace dominant=dom_1 if percentdom_1>=percentdom_2&ambiguous_period==1&percentdom_1!=. & percentdom_2!=.&use_dominant==0 //note the ordering of what winds up in dominant and dominant 2 doesn't really matter.
			replace dominant=dom_2 if percentdom_2>percentdom_1&ambiguous_period==1&percentdom_1!=. & percentdom_2!=.&use_dominant==0
		
			
			replace dominant2=dom_1 if percentdom_1<percentdom_2&ambiguous_period==1&percentdom_1!=. & percentdom_2!=.&use_dominant==0
			replace dominant2=dom_2 if percentdom_2<=percentdom_1&ambiguous_period==1&percentdom_1!=. & percentdom_2!=.&use_dominant==0
		}
		  count if dominant==.&use_dominant==0
		/*local missingdom=r(N)
		if `missingdom'>0&`missingdom'<.{
		summ weekly
		tab week if dominant==.&use_dominant==0
		summ weekly ambiguous_period dom_flag transition_period use_dominant location if dominant==.&use_dominant==0
		STOPMISSINGDOM
		}*/
		
		//calculate the use dominant options before defining transition periods between them
			drop _spell _seq _end
		sort weekly
		gen trans_use_dominant=transition_period
		replace trans_use_dominant=1 if use_dominant==1
		tsspell trans_use_dominant
		gen dominant_before = dominant[_n-1] if _seq==1&trans_use_dominant ==1
		gen dominant_after = dominant[_n+1] if _end==1&trans_use_dominant==1
		egen domb4tmp = mean(dominant_before), by(_spell)
		egen dombaftertmp =mean(dominant_after), by(_spell)
		replace dominant_before = round(domb4tmp,.001)
		replace dominant_after = round(dombaftertmp,.001)
		drop domb4tmp dombaftertmp
		
		replace dominant=dominant_before if use_dominant==1
		replace dominant=dominant_after if use_dominant==1
		}
		// now we can finally define transitions between dominant and ambiguous regions:
				//next we define transitions between dominant periods so that we don't consider these as candidate ambiguous periods.  
		gsort -weekly
		gen transition_period2 = 0 
		gen transition_period3 = 0
		gen transition_mult2 = 0 
		gen transition_mult3 = 0
		gen transition_mult4 = 0 
		gen transition_mult5 = 0

		if _N>1{
				gsort -weekly
		replace transition_period2=1 if (dominant[_n]!=dominant[_n-1]|dominant[_n]!=dominant2[_n-1]|dominant2[_n]!=dominant2[_n-1])&ambiguous_period[_n]==1&dom_flag[_n-1]==1
		replace transition_period2=1 if (dominant[_n]!=dominant[_n-1]|dominant[_n]!=dominant2[_n-1]|dominant2[_n]!=dominant2[_n-1])&ambiguous_period[_n-1]==1&dom_flag[_n]==1
		replace transition_mult2=dominant[_n-1] if (dominant[_n]!=dominant[_n-1]|dominant[_n]!=dominant2[_n-1]|dominant2[_n]!=dominant2[_n-1])&ambiguous_period[_n]==1&dom_flag[_n-1]==1
		replace transition_mult2=dominant[_n-1] if (dominant[_n]!=dominant[_n-1]|dominant[_n]!=dominant2[_n-1]|dominant2[_n]!=dominant2[_n-1])&ambiguous_period[_n-1]==1&dom_flag[_n]==1
		replace transition_mult3=dominant2[_n-1] if (dominant[_n]!=dominant[_n-1]|dominant[_n]!=dominant2[_n-1]|dominant2[_n]!=dominant2[_n-1])&ambiguous_period[_n]==1&dom_flag[_n-1]==1
		replace transition_mult3=dominant2[_n-1] if (dominant[_n]!=dominant[_n-1]|dominant[_n]!=dominant2[_n-1]|dominant2[_n]!=dominant2[_n-1])&ambiguous_period[_n-1]==1&dom_flag[_n]==1
	
		sort weekly
		replace transition_period2=1 if (dominant[_n]!=dominant[_n-1]|dominant[_n]!=dominant2[_n-1]|dominant2[_n]!=dominant2[_n-1])&ambiguous_period[_n]==1&dom_flag[_n-1]==1
		replace transition_period2=1 if (dominant[_n]!=dominant[_n-1]|dominant[_n]!=dominant2[_n-1]|dominant2[_n]!=dominant2[_n-1])&ambiguous_period[_n-1]==1&dom_flag[_n]==1
		replace transition_mult4=dominant[_n-1] if (dominant[_n]!=dominant[_n-1]|dominant[_n]!=dominant2[_n-1]|dominant2[_n]!=dominant2[_n-1])&ambiguous_period[_n]==1&dom_flag[_n-1]==1
		replace transition_mult4=dominant[_n-1] if (dominant[_n]!=dominant[_n-1]|dominant[_n]!=dominant2[_n-1]|dominant2[_n]!=dominant2[_n-1])&ambiguous_period[_n-1]==1&dom_flag[_n]==1
		replace transition_mult5=dominant2[_n-1] if (dominant[_n]!=dominant[_n-1]|dominant[_n]!=dominant2[_n-1]|dominant2[_n]!=dominant2[_n-1])&ambiguous_period[_n]==1&dom_flag[_n-1]==1
		replace transition_mult5=dominant2[_n-1] if (dominant[_n]!=dominant[_n-1]|dominant[_n]!=dominant2[_n-1]|dominant2[_n]!=dominant2[_n-1])&ambiguous_period[_n-1]==1&dom_flag[_n]==1
	
		
		//lead in period (based on first multiplier inventory lot date)
		forvalues j = 1(1)1{
		gsort -weekly
		replace transition_mult2 = transition_mult2[_n-1] if transition_mult2[_n-1]==mult_num`j'&percentfirst_`j'>=.1 &mult_num`j'!=dominant&mult_num`j'!=dominant2&weekly[_n]!=.  //&(mult_num`j'==dominant[_n-1])
		replace transition_mult3 = transition_mult3[_n-1] if transition_mult3[_n-1]==mult_num`j'&percentfirst_`j'>=.1 &mult_num`j'!=dominant &mult_num`j'!=dominant2&weekly[_n]!=.  //&(mult_num`j'==dominant2[_n-1])
		replace transition_mult4 = transition_mult4[_n-1] if transition_mult4[_n-1]==mult_num`j'&percentfirst_`j'>=.1 &mult_num`j'!=dominant&mult_num`j'!=dominant2&weekly[_n]!=.  //&(mult_num`j'==dominant[_n-1])
		replace transition_mult5 = transition_mult5[_n-1] if transition_mult5[_n-1]==mult_num`j'&percentfirst_`j'>=.1 &mult_num`j'!=dominant &mult_num`j'!=dominant2&weekly[_n]!=.  //&(mult_num`j'==dominant2[_n-1])
		
		}
		 
		
		//lead out period (based on last multiplier inventory lot date)
		sort weekly
		forvalues j = 1(1)`finalmult_max'{
		replace transition_mult2 = transition_mult2[_n-1] if transition_mult2[_n-1]==mult_num`j'&percentlast_`j'>=.1 &mult_num`j'!=dominant[_n]&mult_num`j'!=dominant2[_n]&weekly[_n]!=. //&(mult_num`j'==dominant[_n-1])
		replace transition_mult3 = transition_mult3[_n-1] if transition_mult3[_n-1]==mult_num`j'&percentlast_`j'>=.1 &mult_num`j'!=dominant[_n]&mult_num`j'!=dominant2[_n]&weekly[_n]!=. //&(mult_num`j'==dominant2[_n-1])
		replace transition_mult4 = transition_mult4[_n-1] if transition_mult4[_n-1]==mult_num`j'&percentlast_`j'>=.1 &mult_num`j'!=dominant[_n]&mult_num`j'!=dominant2[_n]&weekly[_n]!=. //&(mult_num`j'==dominant[_n-1])
		replace transition_mult5 = transition_mult5[_n-1] if transition_mult5[_n-1]==mult_num`j'&percentlast_`j'>=.1 &mult_num`j'!=dominant[_n]&mult_num`j'!=dominant2[_n]&weekly[_n]!=. //&(mult_num`j'==dominant2[_n-1])
		
		}
 
		replace transition_period2 = 1 if (transition_mult2!=0&transition_mult2<.)|(transition_mult3!=0&transition_mult3<.)|(transition_mult4!=0&transition_mult4<.)|(transition_mult5!=0&transition_mult5<.)
		
		//we may want to expand out the transition periods.  here is the code for that
			gsort -weekly
			gen tmp=transition_mult2[_n-1] if transition_period2[_n-1]==1 &weekly[_n]!=.
			replace  tmp=transition_mult2[_n-2] if transition_period2[_n-2]==1&transition_period2[_n-1]==0 &weekly[_n]!=.
			replace transition_mult2=tmp if tmp!=.&tmp!=0
			drop tmp
			gen tmp=transition_mult3[_n-1] if transition_period2[_n-1]==1 &weekly[_n]!=.
			replace  tmp=transition_mult3[_n-2] if transition_period2[_n-2]==1&transition_period2[_n-1]==0 &weekly[_n]!=.
			replace transition_mult3=tmp if tmp!=.&tmp!=0
			drop tmp
			
			gsort weekly
			gen tmp=transition_mult4[_n-1] if transition_period2[_n-1]==1 &weekly[_n]!=.
			replace  tmp=transition_mult4[_n-2] if transition_period2[_n-2]==1&transition_period2[_n-1]==0 &weekly[_n]!=.
			replace transition_mult4=tmp if tmp!=.&tmp!=0
			drop tmp
			gen tmp=transition_mult5[_n-1] if transition_period2[_n-1]==1 &weekly[_n]!=.
			replace  tmp=transition_mult5[_n-2] if transition_period2[_n-2]==1&transition_period2[_n-1]==0 &weekly[_n]!=.
			replace transition_mult5=tmp if tmp!=.&tmp!=0
			drop tmp

			replace transition_period2 = 1 if (transition_mult2!=0&transition_mult2<.)|(transition_mult3!=0&transition_mult3<.)|(transition_mult4!=0&transition_mult4<.)|(transition_mult5!=0&transition_mult5<.)
		
		//set so that don't start/end on a transition period but rather an ambiguous period
		sort weekly
		gen ambiguous_period2=1 if transition_period2==1&weekly[_n-1]==.
		replace ambiguous_period2=1 if transition_period2==1&ambiguous_period2[_n-1]==1
		replace ambiguous_period=1 if ambiguous_period2==1
		replace transition_period2=0 if ambiguous_period2==1
		drop ambiguous_period2
		gsort -weekly
		gen ambiguous_period2=1 if transition_period2==1&weekly[_n-1]==.
		replace ambiguous_period2=1 if transition_period2==1&ambiguous_period2[_n-1]==1
		replace ambiguous_period=1 if ambiguous_period2==1
		replace transition_period2=0 if ambiguous_period2==1
		
		replace ambiguous_period=0 if transition_period2==1
		replace dom_flag=0 if transition_period2==1|ambiguous_period==1
		replace use_dominant=0 if transition_period2==1
		
		//define ambiguous before and ambiguous after to use in stage 5:
			drop _spell _seq _end
		sort weekly
		tsspell transition_period2
		gen ambiguous_before = dominant[_n-1] if _seq==1&transition_period2 ==1
		gen ambiguous_after = dominant[_n+1] if _end==1&transition_period2==1
		egen domb4tmp = mean(ambiguous_before), by(_spell)
		egen dombaftertmp =mean(ambiguous_after), by(_spell)
		replace ambiguous_before = round(domb4tmp,.001)
		replace ambiguous_after = round(dombaftertmp,.001)
		drop domb4tmp dombaftertmp
		gen ambiguous_before2 = dominant2[_n-1] if _seq==1&transition_period2 ==1
		gen ambiguous_after2 = dominant2[_n+1] if _end==1&transition_period2==1
		egen domb4tmp = mean(ambiguous_before2), by(_spell)
		egen dombaftertmp =mean(ambiguous_after2), by(_spell)
		replace ambiguous_before2 = round(domb4tmp,.001)
		replace ambiguous_after2 = round(dombaftertmp,.001)
		drop domb4tmp dombaftertmp

		drop _spell _seq _end
		}

		/*	// make a plot of percentages from stage 3: 

		sum location 
		local location = r(mean)
		sort weekly
		twoway line percentfirst_* dom_flag transition_period transition_period2 ambiguous_period use_dominant weekly, title(Periods from Stage 3: `location')
		graph export periods_fromstage3_`location'.png, as(png) replace
		*/
	//NEXT STEP: we are now going to say "these are the multipliers that you can consider in any given week"-- trivial for dominant regions, more interesting for ambiguous regions and their transitions.
	//we are creating final options and then zero-ing them out 
	forvalues j = 1(1)16{
		gen final_option_`j' = 0 
		}

	forvalues j = 1(1)`finalmult_max'{
		sum mult_num`j'
		replace final_option_`j' = round(r(mean),.001)
	}
	
	local finalmult_max = 10
	
		//next we deal with dominant periods 
		forvalues j = 1(1)`finalmult_max'{
		replace final_option_`j' = 0 if final_option_`j'!=dominant&dom_flag==1
		}
	
		//next we deal with ambiguous periods 
		if _N>1{
		forvalues j = 1(1)`finalmult_max'{
		replace final_option_`j' = 0 if final_option_`j'!=dominant&final_option_`j'!=dominant2&ambiguous_period==1&use_dominant==0&final_option_`j'!=transition_mult6&final_option_`j'!=transition_mult7
		}
		}
		
		//next we deal with transition periods
		forvalues j = 1(1)`finalmult_max'{
		replace final_option_`j' = 0 if final_option_`j'!=transition_mult6&final_option_`j'!=transition_mult7&final_option_`j'!=dominant&transition_period==1 
		}
		 
		if _N>1{
		//next we deal with transition periods2
		forvalues j = 1(1)`finalmult_max'{
		replace final_option_`j' = 0 if final_option_`j'!=transition_mult2&final_option_`j'!=transition_mult3&final_option_`j'!=transition_mult4&final_option_`j'!=transition_mult5&final_option_`j'!=dominant&final_option_`j'!=dominant2&transition_period2==1 
		}
		

		//use dominant periods

			
		summ dominant_before dominant_after final_option* transition_period
		
		forvalues j = 1(1)`finalmult_max'{
		replace final_option_`j' = 0 if (final_option_`j'!=dominant_before)&(final_option_`j'!=dominant_after)&(final_option_`j'!=dominant)&use_dominant==1
		}
		}
		
		*drop _spell _seq _end
	*STOP
		
		//keep variables we care about/clean up/save file 
	if _N>1{
		keep weekly final_option_* location ambiguous_period dom_flag* use_dominant transition_period* transition_mult* dominant*  ambiguous_before ambiguous_before2  ambiguous_after ambiguous_after2
		tsset weekly 
		tsfill 
		
		tsspell location
		gen dom_flag_before  = dom_flag[_n-1] if _seq==1
			egen tmp = mean(dom_flag_before), by(_spell)
			replace dom_flag_before = tmp
			drop tmp
		gen dom_flag_after = dom_flag[_n+1] if _end==1
			egen tmp = mean(dom_flag_after), by(_spell)
			replace dom_flag_after = tmp
			drop tmp
		
		gen ambiguous_flag_before = ambiguous_period[_n-1] if _seq==1
		replace ambiguous_flag_before = use_dominant[_n-1] if _seq==1&use_dominant[_n-1]==1
			egen tmp = mean(ambiguous_flag_before), by(_spell)
			replace ambiguous_flag_before = tmp
			drop tmp
		gen ambiguous_flag_after = ambiguous_period[_n+1] if _end==1
		replace ambiguous_flag_after = use_dominant[_n+1] if _end==1&use_dominant[_n+1]==1
			egen tmp = mean(ambiguous_flag_after), by(_spell)
			replace ambiguous_flag_after = tmp
			drop tmp
		
		gen trans_before = transition_period[_n-1] if _seq==1
			egen tmp = mean(trans_before), by(_spell)
			replace trans_before = tmp
			drop tmp
		gen trans_after = transition_period[_n+1] if _end==1
			egen tmp = mean(trans_after), by(_spell)
			replace trans_after = tmp
			drop tmp
		
		
		carryforward final_option* dominant* if dom_flag_before==dom_flag_after==1, replace
		carryforward final_option* dominant* if ambiguous_flag_before==ambiguous_flag_after==1, replace
		carryforward final_option* dominant* if trans_before==trans_after==1, replace
		
		//if its bordering regimes 
		carryforward final_option* dominant* if ambiguous_flag_before==1, replace
		carryforward final_option* dominant* if trans_before==1&ambiguous_after==0, replace
		
		gsort -weekly
		carryforward final_option* dominant* if ambiguous_flag_after==1, replace
		carryforward final_option* dominant* if trans_after==1&ambiguous_flag_before==0, replace
		
		//if its the first spell:
		carryforward final_option* dominant* if dom_flag_after==1&_spell==1&dominant==., replace
		
		}

		sort weekly 
	
		save weeklyoptions_`l'.dta, replace 
	
		
		restore 
		
		*/
		//we are back at the transaction level data
		//now merge in these options 

		mmerge weekly using weeklyoptions_`l'.dta, unmatched(master) type(n:1) update replace
 
		erase weeklyoptions_`l'.dta

		sum number_of_total_options
		local round_count = r(mean)
		
		

		//zero out multiplier combinations that don't work
		forvalues j = 1(1)`round_count'{
		gen tmp = mult_`j'
		replace mult_`j' = round(tmp,.001)
		drop tmp 
		}
		
		forvalues j = 1(1)16{
		egen maxweekly=max(final_option_`j'), by(weekly)
		replace final_option_`j'=maxweekly
		drop maxweekly
		}
		

		forvalues j = 1(1)`round_count'{
		*summ round_`j'
		*replace round_`j'=round(round_`j',.001)
		*summ round_`j'
		sort inventoryid date
		 bysort inventoryid: gen tmp = 1 if mult_`j'[1]!=final_option_1[1]& ///
													mult_`j'[1]!=final_option_2[1]& ///
													mult_`j'[1]!=final_option_3[1]& ///
													mult_`j'[1]!=final_option_4[1]& ///
													mult_`j'[1]!=final_option_5[1]& ///
													mult_`j'[1]!=final_option_6[1]& ///
													mult_`j'[1]!=final_option_7[1]& ///
													mult_`j'[1]!=final_option_8[1]& ///
													mult_`j'[1]!=final_option_9[1]& ///
													mult_`j'[1]!=final_option_10[1]& ///
													mult_`j'[1]!=final_option_11[1]& ///
													mult_`j'[1]!=final_option_12[1]& ///
													mult_`j'[1]!=final_option_13[1]& ///
													mult_`j'[1]!=final_option_14[1]& ///
													mult_`j'[1]!=final_option_15[1]& ///
													mult_`j'[1]!=final_option_16[1]
		summ tmp
		replace round_`j' = . if tmp==1
		drop tmp
									}
			 
		/*forvalues j = 1(1)`round_count'{
		sort inventoryid date
		bysort inventoryid: gen round_first_`j' = round_`j'[1]	
		replace round_`j' = round_first_`j' if round_first_`j'==0
		}	*/					
		
		forvalues j = 1(1)`round_count'{
		sort inventoryid date
		bysort inventoryid: gen tmp=1 if mult_`j'[_N]!=final_option_1[_N]& ///
													mult_`j'[_N]!=final_option_2[_N]& ///
													mult_`j'[_N]!=final_option_3[_N]& ///
													mult_`j'[_N]!=final_option_4[_N]& ///
													mult_`j'[_N]!=final_option_5[_N]& ///
													mult_`j'[_N]!=final_option_6[_N]& ///
													mult_`j'[_N]!=final_option_7[_N]& ///
													mult_`j'[_N]!=final_option_8[_N]& ///
													mult_`j'[_N]!=final_option_9[_N]& ///
													mult_`j'[_N]!=final_option_10[_N]& ///
													mult_`j'[_N]!=final_option_11[_N]& ///
													mult_`j'[_N]!=final_option_12[_N]& ///			
													mult_`j'[_N]!=final_option_13[_N]& ///	
													mult_`j'[_N]!=final_option_14[_N]& ///	
													mult_`j'[_N]!=final_option_15[_N]& ///	
													mult_`j'[_N]!=final_option_16[_N]
		summ tmp
		replace round_`j' = . if tmp==1
		drop tmp
										}

		//calc max round 
		sum number_of_total_options
		local round_count = r(mean)
		gen max_round_new= 0
		forvalues j = 1(1)`round_count' {
		replace max_round_new = round_`j' if round_`j'> max_round_new&round_`j'!=.
		}
		
		
		//pick the one that is the most round 
		gen final_mult_new = . 
		forvalues j = 1(1)`round_count' {
		replace final_mult_new = mult_`j' if round_`j'==max_round_new
		}
		
		//see if ties:
		gen count_ties = 0
		forvalues j = 1(1)`round_count' {
		replace count_ties = count_ties+1 if round_`j'==max_round_new
		}
		

		//plot
		tab final_mult_new, gen(multnew)
		egen group_new = group(final_mult_new)
		sum group_new 
		local max = r(max)
		
		sort group_new
		if group_new==.{
		save multiplier_details_`l'.dta, replace
		}
		 if group_new==. continue
		
		format final_mult_new %09.3f
		forvalues r = 1(1)`max' {
		egen sum_mult`r'= sum(multnew`r'), by(weekly)
			sum final_mult_new if group==`r'
			local name = round(r(mean), .001)
			label var  sum_mult`r' "mult = `name'"
		}
		egen sum_ties=sum(count_ties), by(weekly) 
		qui sum location
		local location = r(mean)
		
		save multiplier_details_`l'.dta, replace

		/*plots
		sort weekly
		twoway (line sum_mult* weekly) (line count_ties weekly) || (line dom_flag transition_period transition_period2 ambiguous_period use_dominant weekly, yaxis(2)), ///  
		xline(2886, lcolor(black) lpattern(dash)) title("Stage Four, All Transactions: `location'") ///
		ytitle("Multiplier Count", margin(medsmall))  xtitle("Week")   ///
		graphregion(fcolor(white) lcolor(white) ifcolor(white) ilcolor(white)) ///
		legend(region(lcolor(none))) xlabel(#6, valuelabel angle(forty_five))
		graph export stagefour_`location'.png, as(png) replace
		*/

		}
		
		
